const baseUrl = 'http://localhost:8080'
export const http = ({
	url,
	data,
	params,
	method = 'POST',
	header
}) => {
	method = (method || 'POST').toLocaleLowerCase();
	const token = uni.getStorageSync('token') || ""

	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method,
			data: method === 'get' ? params : data,
			dataType: "json",
			header: { //请求头
				'content-type': 'application/json',
				'authorization': `Bearer ${token}`, //携带token认证字段
				...header
			},
			timeout: 7000, //请求超时时间
			success: (res) => {
				console.log(res)
				if (res.data.code == 200) {
					resolve(res.data);
					return;
				} else if (res.statusCode == 401) {
					uni.showToast({
					  title: "您的身份已过期，请重新登录"
					});
					uni.navigateTo({
						url:"/pages/login/login"
					})
				} else {
					uni.showToast({
					  title: res.data.message,
					  icon: 'error'
					});
					reject(res.data)
				}
			},
			fail: (err) => {
				console.log("---==--=-==-", err)
				uni.navigateTo({
					url:"/pages/login/login"
				})
				reject(err);
			},
			complete: (res) => {}
		})
	})
}
